import com.md.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class stu {
   /* public SqlSession getsqlSession() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }*/

    @Test
    public void sel() throws IOException {
        /*SqlSession sqlSession = getsqlSession();
        List<Student> AllStudent = sqlSession.selectList("sel");
        System.out.println(AllStudent);*/
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = build.openSession();
        List<Student> sel = session.selectList("sel");
        for (Student student : sel) {
            System.out.println(student);
        }
    }

    @Test
    public void insert() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = build.openSession();
        Student student = new Student(0,"zs");
        int insert = session.insert("insert", student);
        session.commit();
        if (insert > 0) {
            System.out.println("成功");
        }
    }

    @Test
    public void update() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = build.openSession();
        Student student = new Student(0,"zs");
        int update = session.update("update", student);
        session.commit();
        if (update > 0) {
            System.out.println("成功");
        }
    }

    @Test
    public void delete() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = build.openSession();
        Student student = new Student(0,"zs");
        int delete = session.delete("del", student);
        session.commit();
        if (delete > 0) {
            System.out.println("成功");
        }
    }
}
